<?php
if(!isset($dbselect)) //前面没设置 数据库 则指定使用 DB3
    $dbselect   = Db::G3();
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and repay_yestime>='$startdate' and  repay_yestime<='$enddate'";  
}




//当日还清标 分站
$rows = $dbselect->GetRows("SELECT r.cid,r.repay_yestime AS '还款时间',COUNT(r.id) AS '次数', SUM(r.repay_capital) AS '本金'
FROM 05b_2list_repay r LEFT JOIN `05b_0base` b ON r.`bid`=b.`bid`
WHERE  ((r.`repay_period`=-1 AND b.borrow_style IN (1,3,4)) OR  (r.`repay_period`=b.`borrow_period` AND b.borrow_style NOT IN (1,3,4))) $repaywhere
GROUP BY r.cid, repay_yestime  ");
foreach($rows as &$row)
{ 
    $cid    = $row['cid'];
    $ctime  = $row['还款时间'];
    $r_c    = $row['本金'];
    $count  = $row['次数']; 
    if($count>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,'借款人_还清标',$r_c,$ctime);
    }
}
//当日还清标 总站
$rows = $dbselect->GetRows("SELECT r.cid,r.repay_yestime AS '还款时间',COUNT(r.id) AS '次数', SUM(r.repay_capital) AS '本金'
FROM 05b_2list_repay r LEFT JOIN `05b_0base` b ON r.`bid`=b.`bid`
WHERE  ((r.`repay_period`=-1 AND b.borrow_style IN (1,3,4)) OR  (r.`repay_period`=b.`borrow_period` AND b.borrow_style NOT IN (1,3,4))) $repaywhere
GROUP BY repay_yestime  ");
foreach($rows as &$row)
{ 
    $ctime  = $row['还款时间'];
    $r_c    = $row['本金'];
    $count  = $row['次数']; 
    if($count>0){
        CTLog::G()->pxxDayReal($count.",sid",1,'借款人_还清标',$r_c,$ctime);
    }
}

//总的统计
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and repay_time>='$startdate' and  repay_time<='$enddate'";  
} 
$rows = $dbselect->GetRows("SELECT cid,repay_time as '待收时间', SUM(repay_account) AS '待收总额', COUNT(repay_account) AS '次数',
SUM(repay_interest) AS '利息', sum(repay_capital) AS '本金',
SUM(repay_sf) AS '其他费用'
FROM 05b_2list_repay
WHERE  1 $repaywhere
GROUP BY cid, repay_time ");
foreach($rows as &$row)
{  
    
    $cid    = $row['cid'];
    $ctime  = $row['待收时间'];
    $r_a    = $row['待收总额'];
    $r_c    = $row['本金'];
    $r_i    = $row['利息'];
    $r_o    = $row['其他费用'];
    $count  = $row['次数']; 
    if($count>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,'借款人_待还_总',$r_a,$ctime);
        CTLog::G()->pxxDayReal($count.",cid",$cid,'借款人_待还_本',$r_c,$ctime);
        CTLog::G()->pxxDayReal($count.",cid",$cid,'借款人_待还_利',$r_i,$ctime);  
    }
}

//体验标统计
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
	$repaywhere    = " and repay_time>='$startdate' and  repay_time<='$enddate'";
}
$sql = "SELECT r.cid,r.repay_time AS '待收时间', SUM(r.repay_account) AS '待收总额', COUNT(r.repay_account) AS '次数',SUM(r.repay_capital) AS '本金'
			,SUM(r.repay_interest) AS '利息'
			FROM 05b_2list_repay AS r
			LEFT JOIN `05b_0base` b ON b.bid=r.bid
			WHERE  1  $repaywhere AND b.flag=5 group by b.cid,repay_time";
	
$datas  = Db::G()->GetRows($sql);
foreach($datas as $data){
    if($data['本金']){
	    CTLog::G()->pxxDayReal($data['次数'].",cid",$data['cid'],'体验金_待收_本金',$data['本金'],$data['待收时间']);
    }
    if($data['利息']){
	    CTLog::G()->pxxDayReal($data['次数'].",cid",$data['cid'],'体验金_待收_利息',$data['利息'],$data['待收时间']);
    }
}


$sql = "select sum(fee) as fee,count(1) as count,date_h from 04a_3applyqueue where aptype=5 and date_h>=? and date_h<=? group by date_h ";
$datas = Db::G()->GetRows($sql,array($startdate,$enddate));
foreach($datas as $data){
    if($data['fee']){
	    CTLog::G()->pxxDayReal($data['count'].",cid",1,'转可提手续费',$data['fee'],$data["date_h"]);
    }
}

$sql = "select sum(money) as money,count(1) as count from 04a_5flow_uid where flowty='huodongjiangli' and objtype2='cid' and qdate>=? and qdate<=? ";
$datas = Db::G()->GetRows($sql,array($startdate,$enddate));
foreach($datas as $data){
    if($data['money']){
	    CTLog::G()->pxxDayReal($data['count'].",cid",1,'红包使用支出费用',$data['money'],$data["date_h"]);
    }
}


///总站借款人待收余额
$rows = $dbselect->GetRows("SELECT  SUM(repay_account) AS '待收总额', COUNT(repay_account) AS '次数',
SUM(repay_interest) AS '利息', sum(repay_capital) AS '本金',fuid_GetCid(uid) as cid
FROM 05b_2list_repay
WHERE  (repay_status=0 or repay_status=1 and repay_yestime>='$enddate')");
foreach($rows as &$row)
{   
    $ctime  = $curday;
    $r_a    = $row['待收总额'];
    $r_c    = $row['本金'];
    $r_i    = $row['利息'];
    $count  = $row['次数'];
    $cid	= $row["cid"];
    
    if($count>0){
        CTLog::G()->pxxDayReal($count.",sid",1,'借款人_待还_余_总',$r_a,$ctime);
        CTLog::G()->pxxDayReal($count.",sid",1,'借款人_待还_余_本',$r_c,$ctime);
        CTLog::G()->pxxDayReal($count.",sid",1,'借款人_待还_余_利',$r_i,$ctime);
    }
}


///分站：借款人待收余额
$rows = $dbselect->GetRows("SELECT  SUM(repay_account) AS '待收总额', COUNT(repay_account) AS '次数',
		SUM(repay_interest) AS '利息', sum(repay_capital) AS '本金',fuid_GetCid(uid) as cid
		FROM 05b_2list_repay
		WHERE  (repay_status=0 or repay_status=1 and repay_yestime>='$enddate') GROUP BY cid ");
foreach($rows as &$row)
{
	$ctime  = $curday;
	$r_a    = $row['待收总额'];
	$r_c    = $row['本金'];
	$r_i    = $row['利息'];
	$count  = $row['次数'];
	$cid	= $row["cid"];

	if($count>0){
		CTLog::G()->pxxDayReal($count.",cid",$cid,'借款人_待还_余_总',$r_a,$ctime);
		CTLog::G()->pxxDayReal($count.",cid",$cid,'借款人_待还_余_本',$r_c,$ctime);
		CTLog::G()->pxxDayReal($count.",cid",$cid,'借款人_待还_余_利',$r_i,$ctime);
	}
}

//正常还款的
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and  repay_yestime>='$startdate' and  repay_yestime<='$enddate'";  
}

$rows = $dbselect->GetRows("SELECT cid,repay_yestime as '还款时间', SUM(repay_account_yes) AS '还款总额', COUNT(repay_account_yes) AS '次数',
SUM(repay_interest_yes) AS '还款利息', sum(repay_capital_yes) AS '还款本金',SUM(repay_sf) AS '收取的费用',SUM(late_interest) AS '逾期罚息',
sum(if(late_interest>0,1,0)) AS '罚息次数',SUM(late_reminder) AS '逾期催收费', sum(if(late_reminder>0,1,0)) AS '逾期催收费' 
FROM 05b_2list_repay
WHERE repay_status=1 and isdianzi=0 and repay_web=0 $repaywhere
GROUP BY cid, repay_yestime ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = $row['还款时间'];
    $r_a    = $row['还款总额'];
    $r_c    = $row['还款本金'];
    $r_i    = $row['还款利息'];
    $r_li   = $row['逾期罚息'];
    $r_lr   = $row['逾期催收费'];
    $count  = $row['次数'];
    $c_li   = $row['罚息次数'];
    $c_lr   = $row['逾期催收费'];

    if($r_a>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"借款人_已还_总",$r_a,$ctime); //分站统计 
    }
    if($r_c>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"借款人_已还_本",$r_c,$ctime); //分站统计   
    }
    if($r_i>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"借款人_已还_利息",$r_i,$ctime); //分站统计  
    }
    if($r_li>0){
        CTLog::G()->pxxDayReal($c_li.",cid",$cid,"借款人_已还_罚息",$r_li,$ctime); //分站统计 
    }
    if($r_lr>0){
        CTLog::G()->pxxDayReal($c_lr.",cid",$cid,"借款人_已还_催收费",$r_lr,$ctime); //分站统计
    }
}

//垫付还款
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and  repay_yestime>='$startdate' and  repay_yestime<='$enddate'";  
}

$rows = $dbselect->GetRows("SELECT cid,repay_yestime as '垫付时间', 
SUM(repay_account_yes) AS '还款总额', COUNT(repay_account_yes) AS '次数',
SUM(repay_interest_yes) AS '还款利息',sum(repay_capital_yes) AS '还款本金',SUM(repay_sf) AS '收取的费用',
sum(dianfu_money) as '垫付金额',sum(dianfu_capital) as '垫付本金',sum(dianfu_sf) as '垫付收费'  
FROM 05b_2list_repay
WHERE repay_status=1 and repay_web=1 $repaywhere
GROUP BY cid, repay_yestime ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = $row['垫付时间'];
    $r_a    = $row['还款总额'];
    $r_c    = $row['还款本金'];
    $r_i    = $row['还款利息'];
    $count  = $row['次数'];
    if($r_a>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫付_标_总",$r_a,$ctime); //分站统计
    }
    if($r_c>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫付_标_本",$r_c,$ctime); //分站统计   
    }
    if($r_i>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫付_标_利",$r_i,$ctime); //分站统计  
    }
    
    if($row['垫付金额']*1>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫付_实_总",$row['垫付金额'],$ctime); //分站统计   垫付总额
    }
    if($row['垫付本金']>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫付_实_本",$row['垫付本金'],$ctime); //分站统计   垫付总额
    }
    if($row['垫付收费']>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫付_实_其他",$row['垫付收费'],$ctime); //分站统计   垫付总额
    }
}

//垫资还款
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and  repay_yestime>='$startdate' and  repay_yestime<='$enddate'";  
}

$rows = $dbselect->GetRows("SELECT cid,repay_yestime as '垫资时间',
SUM(repay_account_yes) AS '还款总额', COUNT( repay_account_yes ) AS '次数',
SUM(repay_interest_yes) AS '还款利息',sum(repay_capital_yes) AS '还款本金',
SUM(repay_sf) AS '收取的费用',sum(dianzi_money) as '垫资金额' 
FROM 05b_2list_repay
WHERE repay_status=1 and isdianzi=1 $repaywhere
GROUP BY cid, repay_yestime ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = $row['垫资时间'];
    $r_a    = $row['还款总额'];
    $r_c    = $row['还款本金'];
    $r_i    = $row['还款利息'];
    $r_dz   = $row['垫资金额'];
    $count  = $row['次数'];

    if($r_a*1<>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫资_标_总",$r_a,$ctime); //分站统计    
    }
    if($r_c*1<>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫资_标_本",$r_c,$ctime); //分站统计   
    }
    if($r_i*1<>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫资_标_本",$r_i,$ctime); //分站统计  
    }
    if($r_dz*1<>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫资_实_总",$r_dz,$ctime); //分站统计
    }  
}

//垫付还款收回
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and  repay_backtime>='$startdate' and  repay_backtime<='$enddate'";  
}

$rows = $dbselect->GetRows("SELECT cid,SUBSTR( repay_backtime, 1, 10 ) as '收回垫付款',sum(dianfu_money) as '垫付金额',COUNT(dianfu_money) AS '次数',
sum(if(csf>0,csf,0)) as '催收费',sum(dianfu_money+csf) as '收回垫付金额',sum(if(csf>=0,0,-csf)) as '垫付损失',
sum(if(csf>0,1,0)) as '催收费次数',sum(if(csf<0,1,0)) as '损失次数'
FROM 05b_2list_repay
WHERE repay_status=1 and repay_web=1 and repay_backstate=1 $repaywhere
GROUP BY cid, SUBSTR( repay_backtime, 1, 10 ) ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = $row['收回垫付款'];
    $count  = $row['次数'];
    $Money  = $row['收回垫付金额'];
    $CS     = $row['催收费'];
    $c_CS   = $row['催收费次数'];
    $SS     = $row['垫付损失'];
    $c_SS   = $row['损失次数'];
    if($count>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫付_实_收",$Money,$ctime);    //分站垫付收回统计
    }
    if($c_SS>0){
        CTLog::G()->pxxDayReal($c_SS.",cid",$cid,"垫付_实_损失",$SS,$ctime);     //分站垫付收损失
    }
    if($c_CS>0){
        CTLog::G()->pxxDayReal($c_CS.",cid",$cid,"垫付_实_催收",$CS,$ctime);     //分站垫付崔收费
    }
}



//垫资还款收回
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
    $repaywhere    = " and  repay_backtime>='$startdate' and  repay_backtime<='$enddate'";  
}

$rows = $dbselect->GetRows("SELECT cid,SUBSTR( repay_backtime, 1, 10 ) as '收回垫资款',sum(dianzi_money) as '垫资金额',COUNT(repay_account_yes) AS '次数',
sum(if(csf>0,csf,0)) as '催收费',sum(dianzi_money+csf) as '收回垫资金额',sum(if(csf>=0,0,-csf)) as '垫资损失',
sum(if(csf>0,1,0)) as '催收费次数',sum(if(csf<0,1,0)) as '损失次数'
FROM 05b_2list_repay
WHERE repay_status=1 and isdianzi=1 and repay_backstate=1 $repaywhere
GROUP BY cid, SUBSTR( repay_backtime, 1, 10 ) ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = $row['收回垫资款'];
    $count  = $row['次数'];
    $Money  = $row['收回垫资金额'];
    $CS     = $row['催收费'];
    $c_CS   = $row['催收费次数'];
    $SS     = $row['垫资损失'];
    $c_SS   = $row['损失次数'];
    if($count>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫资_实_收",$Money,$ctime);    //分站垫资收回统计
    }
    if($c_SS>0){
        CTLog::G()->pxxDayReal($c_SS.",cid",$cid,"垫资_实_损失",$SS,$ctime);           //分站垫资收损失
    }
    if($c_CS>0){
        CTLog::G()->pxxDayReal($c_CS.",cid",$cid,"垫资_实_催收",$CS,$ctime);          //分站垫资崔收费
    }
}

///垫付款余额

$rows = $dbselect->GetRows("SELECT cid,sum(dianfu_money) as '垫付金额',COUNT(dianfu_money) AS '次数'
FROM 05b_2list_repay
WHERE repay_status=1 and repay_web=1 and (repay_backstate=0 or repay_backtime>='$enddate')
GROUP BY cid ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = $curday;
    $count  = $row['次数'];
    $Money  = $row['垫付金额'];
    if($count>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫付_当前_余额",$Money,$ctime);    //分站垫付收回统计
    }
}


///垫资款余额
$rows = $dbselect->GetRows("SELECT cid,sum(dianzi_money) as '垫资金额',COUNT(dianzi_money) AS '次数'
FROM 05b_2list_repay
WHERE repay_status=1 and isdianzi=1 and (repay_backstate=0 or repay_backtime>='$enddate') 
GROUP BY cid ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = $curday;
    $count  = $row['次数'];
    $Money  = $row['垫资金额'];
    if($count>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"垫资_当前_余额",$Money,$ctime);    //分站垫资收回统计
    }
}

///完成

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//总的统计
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and repay_time>='$startdate' and  repay_time<='$enddate'";  
} 
$rows = $dbselect->GetRows("SELECT repay_time as '待收时间', SUM(repay_account) AS '待收总额', COUNT(repay_account) AS '次数',
SUM(repay_interest) AS '利息', sum(repay_capital) AS '本金',
SUM(repay_sf) AS '其他费用'
FROM 05b_2list_repay
WHERE  1 $repaywhere
GROUP BY repay_time ");
foreach($rows as &$row)
{   
    $ctime  = $row['待收时间'];
    $r_a    = $row['待收总额'];
    $r_c    = $row['本金'];
    $r_i    = $row['利息'];
    $r_o    = $row['其他费用'];
    $count  = $row['次数'];
    if($count>0){
        CTLog::G()->pxxDayReal($count.",sid",1,'借款人_待还_总',$r_a,$ctime);
        CTLog::G()->pxxDayReal($count.",sid",1,'借款人_待还_本',$r_c,$ctime);
        CTLog::G()->pxxDayReal($count.",sid",1,'借款人_待还_利',$r_i,$ctime);  
    }
}

//正常还款的
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and  repay_yestime>='$startdate' and  repay_yestime<='$enddate'";  
}

$rows = $dbselect->GetRows("SELECT repay_yestime as '还款时间', SUM(repay_account_yes) AS '还款总额', COUNT(repay_account_yes) AS '次数',
SUM(repay_interest_yes) AS '还款利息', sum(repay_capital_yes) AS '还款本金',SUM(repay_sf) AS '收取的费用',SUM(late_interest) AS '逾期罚息',
sum(if(late_interest>0,1,0)) AS '罚息次数',SUM(late_reminder) AS '逾期催收费', sum(if(late_reminder>0,1,0)) AS '逾期催收费',
COUNT(DISTINCT uid) AS 回款人数
FROM 05b_2list_repay
WHERE repay_status=1 and isdianzi=0 and repay_web=0 $repaywhere
GROUP BY  repay_yestime ");
foreach($rows as &$row)
{   
    $ctime  = $row['还款时间'];
    $r_a    = $row['还款总额'];
    $r_c    = $row['还款本金'];
    $r_i    = $row['还款利息'];
    $r_li   = $row['逾期罚息'];
    $r_lr   = $row['逾期催收费'];
    $count  = $row['次数'];
    $chkrs  = $row['回款人数'];
    $c_li   = $row['罚息次数'];
    $c_lr   = $row['逾期催收费'];

    if($chkrs>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"借款人_还款_人数",$chkrs,$ctime); //分站统计 
    }
    if($r_a>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"借款人_还款_总",$r_a,$ctime); //分站统计 
    }
    if($r_c>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"借款人_还款_本",$r_c,$ctime); //分站统计   
    }
    if($r_i>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"借款人_还款_利息",$r_i,$ctime); //分站统计  
    }
    if($r_li>0){
        CTLog::G()->pxxDayReal($c_li.",sid",1,"借款人_还款_罚息",$r_li,$ctime); //分站统计 
    }
    if($r_lr>0){
        CTLog::G()->pxxDayReal($c_lr.",sid",1,"借款人_还款_催收费",$r_lr,$ctime); //分站统计
    }
}

//垫付还款
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and  repay_yestime>='$startdate' and  repay_yestime<='$enddate'";  
}

$rows = $dbselect->GetRows("SELECT repay_yestime as '垫付时间', SUM(repay_account_yes) AS '还款总额', COUNT(repay_account_yes) AS '次数',
SUM(repay_interest_yes) AS '还款利息',sum(repay_capital_yes) AS '还款本金',SUM(repay_sf) AS '收取的费用',
sum(dianfu_money) as '垫付金额',sum(dianfu_capital) as '垫付本金',sum(dianfu_sf) as '垫付收费'  
FROM 05b_2list_repay
WHERE repay_status=1 and repay_web=1 $repaywhere
GROUP BY  repay_yestime ");
foreach($rows as &$row)
{   
    $ctime  = $row['垫付时间'];
    $r_a    = $row['还款总额'];
    $r_c    = $row['还款本金'];
    $r_i    = $row['还款利息'];
    $count  = $row['次数'];
    if($r_a>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫付_标_总",$r_a,$ctime); //分站统计
    }
    if($r_c>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫付_标_本",$r_c,$ctime); //分站统计   
    }
    if($r_i>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫付_标_利",$r_i,$ctime); //分站统计  
    }
    
    if($row['垫付金额']*1>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫付_实_总",$row['垫付金额'],$ctime); //分站统计   垫付总额
    }
    if($row['垫付本金']>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫付_实_本",$row['垫付本金'],$Y,$M,$D); //分站统计   垫付总额
    }
    if($row['垫付收费']>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫付_实_其他",$row['垫付收费'],$Y,$M,$D); //分站统计   垫付总额
    }
}

//垫资还款
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and  repay_yestime>='$startdate' and  repay_yestime<='$enddate'";  
}

$rows = $dbselect->GetRows("SELECT repay_yestime as '垫资时间', SUM(repay_account_yes) AS '还款总额', COUNT( repay_account_yes ) AS '次数',
SUM(repay_interest_yes) AS '还款利息',sum(repay_capital_yes) AS '还款本金',SUM(repay_sf) AS '收取的费用',sum(dianzi_money) as '垫资金额' 
FROM 05b_2list_repay
WHERE repay_status=1 and isdianzi=1 $repaywhere
GROUP BY  repay_yestime ");
foreach($rows as &$row)
{   
    $ctime  = $row['垫资时间'];
    $r_a    = $row['还款总额'];
    $r_c    = $row['还款本金'];
    $r_i    = $row['还款利息'];
    $r_dz   = $row['垫资金额'];
    $count  = $row['次数'];

    if($r_a*1<>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫资_标_总",$r_a,$ctime); //分站统计    
    }
    if($r_c*1<>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫资_标_本",$r_c,$ctime); //分站统计   
    }
    if($r_i*1<>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫资_标_本",$r_i,$ctime); //分站统计  
    }
    if($r_dz*1<>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫资_实_总",$r_dz,$ctime); //分站统计
    }  
}

//垫付还款收回
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and  repay_backtime>='$startdate' and  repay_backtime<='$enddate'";  
}

$rows = $dbselect->GetRows("SELECT SUBSTR( repay_backtime, 1, 10 ) as '收回垫付款',sum(dianfu_money) as '垫付金额',COUNT(dianfu_money) AS '次数',
sum(if(csf>0,csf,0)) as '催收费',sum(dianfu_money+csf) as '收回垫付金额',sum(if(csf>=0,0,-csf)) as '垫付损失',
sum(if(csf>0,1,0)) as '催收费次数',sum(if(csf<0,1,0)) as '损失次数'
FROM 05b_2list_repay
WHERE repay_status=1 and repay_web=1 and repay_backstate=1 $repaywhere
GROUP BY  SUBSTR( repay_backtime, 1, 10 ) ");
foreach($rows as &$row)
{   
    $ctime  = $row['收回垫付款'];
    $count  = $row['次数'];
    $Money  = $row['收回垫付金额'];
    $CS     = $row['催收费'];
    $c_CS   = $row['催收费次数'];
    $SS     = $row['垫付损失'];
    $c_SS   = $row['损失次数'];
    if($count>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫付_实_收",$Money,$ctime);    //分站垫付收回统计
    }
    if($c_SS>0){
        CTLog::G()->pxxDayReal($c_SS.",sid",1,"垫付_实_损失",$SS,$ctime);     //分站垫付收损失
    }
    if($c_CS>0){
        CTLog::G()->pxxDayReal($c_CS.",sid",1,"垫付_实_催收",$CS,$ctime);     //分站垫付崔收费
    }
}



//垫资还款收回
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
    $repaywhere    = " and  repay_backtime>='$startdate' and  repay_backtime<='$enddate'";  
}

$rows = $dbselect->GetRows("SELECT SUBSTR( repay_backtime, 1, 10 ) as '收回垫资款',sum(dianzi_money) as '垫资金额',COUNT(repay_account_yes) AS '次数',
sum(if(csf>0,csf,0)) as '催收费',sum(dianfu_money+csf) as '收回垫资金额',sum(if(csf>=0,0,-csf)) as '垫资损失',
sum(if(csf>0,1,0)) as '催收费次数',sum(if(csf<0,1,0)) as '损失次数'
FROM 05b_2list_repay
WHERE repay_status=1 and isdianzi=1 and repay_backstate=1 $repaywhere
GROUP BY  SUBSTR( repay_backtime, 1, 10 ) ");
foreach($rows as &$row)
{   
    $ctime  = $row['收回垫资款'];
    $count  = $row['次数'];
    $Money  = $row['收回垫资金额'];
    $CS     = $row['催收费'];
    $c_CS   = $row['催收费次数'];
    $SS     = $row['垫资损失'];
    $c_SS   = $row['损失次数'];
    if($count>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫资_实_收",$Money,$ctime);    //分站垫资收回统计
    }
    if($c_SS>0){
        CTLog::G()->pxxDayReal($c_SS.",sid",1,"垫资_实_损失",$SS,$ctime);           //分站垫资收损失
    }
    if($c_CS>0){
        CTLog::G()->pxxDayReal($c_CS.",sid",1,"垫资_实_催收",$CS,$ctime);          //分站垫资崔收费
    }
}
///垫付款余额

$rows = $dbselect->GetRows("SELECT cid,sum(dianfu_money) as '垫付金额',COUNT(dianfu_money) AS '次数'
FROM 05b_2list_repay
WHERE repay_status=1 and repay_web=1 and (repay_backstate=0 or repay_backtime>'$enddate') ");
foreach($rows as &$row)
{   
   
    $ctime  = $curday;
    $count  = $row['次数'];
    $Money  = $row['垫付金额'];
    if($count>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫付_当前_余额",$Money,$ctime);    //分站垫付收回统计
    }
}


///垫资款余额
$rows = $dbselect->GetRows("SELECT cid,sum(dianzi_money) as '垫资金额',COUNT(dianzi_money) AS '次数'
FROM 05b_2list_repay
WHERE repay_status=1 and isdianzi=1 and (repay_backstate=0 or repay_backtime>'$enddate')");
foreach($rows as &$row)
{   
    $ctime  = $curday;
    $count  = $row['次数'];
    $Money  = $row['垫资金额'];
    if($count>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"垫资_当前_余额",$Money,$ctime);    //分站垫资收回统计
    }
}

//体验标统计
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
	$repaywhere    = " and repay_time>='$startdate' and  repay_time<='$enddate'";
}
$sql = "SELECT r.cid,r.repay_time AS '待收时间', SUM(r.repay_account) AS '待收总额', COUNT(r.repay_account) AS '次数',SUM(r.repay_capital) AS '本金'
            ,SUM(r.repay_interest) AS '利息'
            FROM 05b_2list_repay AS r
            LEFT JOIN `05b_0base` b ON b.bid=r.bid
            WHERE  1  $repaywhere AND b.flag=5 group by repay_time";

$datas  = Db::G()->GetRows($sql);
foreach($datas as $data){
    if($data['本金']){
        CTLog::G()->pxxDayReal($data['次数'].",sid",1,'体验金_待收_本金',$data['本金'],$data['待收时间']);
    }
    if($data['利息']){
        CTLog::G()->pxxDayReal($data['次数'].",sid",1,'体验金_待收_利息',$data['利息'],$data['待收时间']);
    }
}


$sql = "select sum(fee) as fee,count(1) as count,date_h from 04a_3applyqueue where aptype=5 and date_h>=?  and date_h<? group by date_h";

$datas = Db::G()->GetRows($sql,array($startdate,$enddate));
foreach($datas as $data){
    if($data['money']){
        CTLog::G()->pxxDayReal($data['count'].",sid",1,'转可提手续费',$data['money'],$data['date_h']);
        CTLog::G()->pxxDayReal($data['count'].",cid",1,'红包使用支出费用',$data['money'],$data['date_h']);
    }
}

$sql = "select sum(money) as money,count(1) count,qdate from 04a_5flow_uid where flowty='huodongjiangli' and objtype2='cid' and qdate>=?  and  qdate<=? group by qdate";
$datas = Db::G()->GetRows($sql,array($startdate,$enddate));
foreach($datas as $data){
    if($data['money']){
	    CTLog::G()->pxxDayReal($data['count'].",sid",1,'红包使用支出费用',$data['money'],$data['qdate']);
        CTLog::G()->pxxDayReal($data['count'].",cid",1,'红包使用支出费用',$data['money'],$data['qdate']);
    }
}



